package com.zy.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zy.entity.Resource;
import com.zy.mapper.ResourceMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class ResourceController {
    @Autowired
    private ResourceMapper resourceMapper;

    /**
     * 查询所有会议
     */
    @RequestMapping("/listMetting")
    public ModelAndView addMetting(@RequestParam(value = "pageNo", defaultValue = "1") Integer pageNo,
                                   @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize) {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("MettingManage");

        //会议分页
        IPage iPage = new Page<>(pageNo, pageSize);
        IPage mettings = resourceMapper.selectPage(iPage, null);
        modelAndView.addObject("mettings", mettings);
        return modelAndView;
    }

    /**
     * 添加会议
     */
    @RequestMapping("/addMetting")
    public String addMetting(Resource resource,
                             @RequestParam(value = "pageNo", defaultValue = "1") Integer pageNo,
                             @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize) {
        //添加会议
        resourceMapper.addMetting(resource);
        return "redirect:/listMetting?pageNo=" + pageNo;
    }

    /**
     * 删除会议
     */
    @RequestMapping("/deleteMetting")
    @ResponseBody
    public String deleteMetting(Integer id) {
        resourceMapper.deleteMetting(id);
        return "";
    }

    /**
     * 修改会议
     */
    @RequestMapping("/modifyMetting")
    public String modifyMetting(Resource resource,
                                @RequestParam(value = "pageNo", defaultValue = "1") Integer pageNo,
                                @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize) {

        resourceMapper.modifyMetting(resource);
        return "redirect:/listMetting?pageNo=" + pageNo;
    }

}
